package com.lmqb.service.impl;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmqb.bo.PassRateBo;
import com.lmqb.bo.PassRateExport;
import com.lmqb.dao.DataDailyMapper;
import com.lmqb.dao.DataSummaryMapper;
import com.lmqb.service.DataDailyService;
import com.lmqb.util.DataUtils;
import com.lmqb.util.ExportUtil;

@Service("dataDailyService")
public class DataDailyServiceImpl implements DataDailyService{
	@Autowired
	private DataDailyMapper dataDailyDao;
	@Autowired
	private DataSummaryMapper dataSummaryDao;

	@Override
	public Map<String, Object> selectDataDaily() {
		Map<String, Object> map = new HashMap<>();
		Integer RegisterToday = dataDailyDao.selectRegisterToday();
		map.put("RegisterToday", RegisterToday);
		Integer LoanToday = dataDailyDao.selectLoanToday();
		map.put("LoanToday", LoanToday);
		Integer JishenPass = dataDailyDao.selectJishenPass();
		map.put("JishenPass", JishenPass);
		Integer JishenNoPass = dataDailyDao.selectJishenNoPass();
		map.put("JishenNoPass", JishenNoPass);
		Integer ZhongshenPass = dataDailyDao.selectZhongshenPass();
		map.put("ZhongshenPass", ZhongshenPass);
		Integer RegisterAll = dataDailyDao.selectRegisterAll();
		map.put("RegisterAll", RegisterAll);
		BigDecimal FangkuanToday = dataDailyDao.selectFangkuanToday();
		map.put("FangkuanToday", interceptDecimal(FangkuanToday));
		BigDecimal FangkuanAll = dataDailyDao.selectFangkuanAll();
		map.put("FangkuanAll", interceptDecimal(FangkuanAll));
		BigDecimal HuankuanAll = dataDailyDao.selectHuankuanAll();
		map.put("HuankuanAll", interceptDecimal(HuankuanAll));
		BigDecimal HuankuanToday = dataDailyDao.selectHuankuanToday();
		map.put("HuankuanToday", interceptDecimal(HuankuanToday));
		BigDecimal HuankuanMoney = dataDailyDao.selectHuankuanMoney();
		map.put("HuankuanMoney", interceptDecimal(HuankuanMoney));
		Integer YuqiAll = dataDailyDao.selectYuqiAll();
		map.put("YuqiAll", YuqiAll);
		Integer Yuqiing = dataDailyDao.selectYuqiing();
		map.put("Yuqiing", Yuqiing);
		BigDecimal YuqiMoney = dataDailyDao.selectYuqiMoney();
		map.put("YuqiMoney", interceptDecimal(YuqiMoney));
		BigDecimal YuqiLatefee = dataDailyDao.selectYuqiLatefee();
		map.put("YuqiLatefee", interceptDecimal(YuqiLatefee));
		
		Integer ChushenPass = dataDailyDao.selectChushenPass();
		map.put("ChushenPass", ChushenPass);
		Integer ChushenNoPass = dataDailyDao.selectChushenNoPass();
		map.put("ChushenNoPass", ChushenNoPass);
		Integer FushenPass = dataDailyDao.selectFushenPass();
		map.put("FushenPass", FushenPass);
		Integer FushenNoPass = dataDailyDao.selectFushenNoPass();
		map.put("FushenNoPass", FushenNoPass);
		Integer ZhongshenNoPass = dataDailyDao.selectZhongshenNoPass();
		map.put("ZhongshenNoPass", ZhongshenNoPass);
		
		//conversionRate申请转化率=今日注册用户/今日申请借款*100%
		//creditRate放款转化率=终审通过/今日申请借款*100%
		double conversionRate = (RegisterToday == 0?0:((LoanToday*10000)/RegisterToday)*0.01);
		map.put("conversionRate", conversionRate);
		double creditRate = (LoanToday == 0?0:((ZhongshenPass*10000)/LoanToday)*0.01);
		map.put("creditRate", creditRate);
		
		return map;
	}
	/**
	 * 截取BigDecimal两位小数
	 * @param bigDecimal
	 * @return
	 */
	public BigDecimal interceptDecimal(BigDecimal bigDecimal){
		if(bigDecimal == null){
			bigDecimal = new BigDecimal("0.00");
		}else{
			bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
		}
		return bigDecimal;
	}
	@Override
	public Map<String, Object> selectPaseRate(Integer page, Integer pageSize) {
		if(page == null || page <= 0){
			page = 1;
		}
		if(pageSize == null || pageSize == 0){
			pageSize = 10;
		}
		Map<String, Object> map = new HashMap<>();
		map.put("page", page);
		map.put("pageSize", pageSize);
		
		//获取总条数
		Integer totalRow = dataDailyDao.selectCountPaseRate(map);
		map.put("totalRow", totalRow);//总共有多少条记录
		
		Integer totalPage= (totalRow % pageSize != 0 )?(totalRow/pageSize+1):(totalRow/pageSize); //总页数。
		if(page > totalPage){
			page = totalPage;
			map.put("page", page);//防止页面在最后一页时点击下一页
		}
		Integer offset = (page-1)*pageSize;	//偏移量。从第几条数据开始
		map.put("offset", offset);
		map.put("totalPage", totalPage);
		
		List<PassRateBo> list = dataDailyDao.selectAllPaseRate(map);
		if(list != null &&  list.size() !=0){
			for (PassRateBo passRateBo : list) {
				String date = DataUtils.formDate2Str(passRateBo.getTj_date()+"");
				passRateBo.setDate(date);
				passRateBo.setTotal_credit(interceptDecimal(passRateBo.getTotal_credit()));
				passRateBo.setLending_money(interceptDecimal(passRateBo.getLending_money()));
			}
		}
		map.put("list", list);
		return map;
	}
	@Override
	public Workbook selectPaseRateToExcel() {
		List<PassRateExport> list = dataDailyDao.selectPaseRateToExcel();
        if(list!=null && list.size()!=0){
        	for (PassRateExport passRateExport : list) {
				passRateExport.setTj_date(DataUtils.formDate2Str(passRateExport.getTj_date()+""));
				passRateExport.setTotal_credit(interceptDecimal(passRateExport.getTotal_credit()));
				passRateExport.setLending_money(interceptDecimal(passRateExport.getLending_money()));
			}
            Workbook workbook = ExportUtil.exportExcel("审核数据列表",PassRateExport.class, list);
            return workbook;
        }
		return null;
	}
	
	@Override
	public Map<String, Object> selectSummary() {
		Map<String, Object> map = new HashMap<>();
		Integer todayRegister = dataSummaryDao.selectTodayRegister();
		Integer yesterdayRegister = dataSummaryDao.selectYesterdayRegister();
		Integer tswkRegister = dataSummaryDao.selectTswkRegister();
		Integer monthRegister = dataSummaryDao.selectMonthRegister();
		Integer totalRegister = dataSummaryDao.selectTotalRegister();
		
		map.put("todayRegister", todayRegister);
		map.put("yesterdayRegister", yesterdayRegister);
		map.put("tswkRegister", tswkRegister);
		map.put("monthRegister", monthRegister);
		map.put("totalRegister", totalRegister);
		
		Integer todayApplyTotal = dataSummaryDao.selectTodayApplyTotal();
		Integer yesterdayApplyTotal = dataSummaryDao.selectYesterdayApplyTotal();
		Integer tswkApplyTotal = dataSummaryDao.selectTswkApplyTotal();
		Integer monthApplyTotal = dataSummaryDao.selectMonthApplyTotal();
		Integer totalApplyTotal = dataSummaryDao.selectTotalApplyTotal();
		
		map.put("todayApplyTotal", todayApplyTotal);
		map.put("yesterdayApplyTotal", yesterdayApplyTotal);
		map.put("tswkApplyTotal", tswkApplyTotal);
		map.put("monthApplyTotal", monthApplyTotal);
		map.put("totalApplyTotal", totalApplyTotal);
		
		Integer todayApplyPass = dataSummaryDao.selectTodayApplyPass();
		Integer yesterdayApplyPass = dataSummaryDao.selectYesterdayApplyPass();
		Integer tswkApplyPass = dataSummaryDao.selectTswkApplyPass();
		Integer monthApplyPass = dataSummaryDao.selectMonthApplyPass();
		Integer totalApplyPass = dataSummaryDao.selectTotalApplyPass();
		map.put("todayApplyPass", todayApplyPass); 
		map.put("yesterdayApplyPass", yesterdayApplyPass); 
		map.put("tswkApplyPass", tswkApplyPass); 
		map.put("monthApplyPass", monthApplyPass); 
		map.put("totalApplyPass", totalApplyPass); 
		
		Integer todayApplyNopass = dataSummaryDao.selectTodayApplyNopass();
		Integer yesterdayApplyNopass = dataSummaryDao.selectYesterdayApplyNopass();
		Integer tswkApplyNopass = dataSummaryDao.selectTswkApplyNopass();
		Integer monthApplyNopass = dataSummaryDao.selectMonthApplyNopass();
		Integer totalApplyNopass = dataSummaryDao.selectTotalApplyNopass();
		map.put("todayApplyNopass", todayApplyNopass); 
		map.put("yesterdayApplyNopass", yesterdayApplyNopass); 
		map.put("tswkApplyNopass", tswkApplyNopass); 
		map.put("monthApplyNopass", monthApplyNopass); 
		map.put("totalApplyNopass", totalApplyNopass); 
		 
		Integer todayInitialAuditPass = dataSummaryDao.selectTodayInitialAuditPass();
		Integer yesterdayInitialAuditPass = dataSummaryDao.selectYesterdayInitialAuditPass();
		Integer tswkInitialAuditPass = dataSummaryDao.selectTswkInitialAuditPass();
		Integer monthInitialAuditPass = dataSummaryDao.selectMonthInitialAuditPass();
		Integer totalInitialAuditPass = dataSummaryDao.selectTotalInitialAuditPass();
		map.put("todayInitialAuditPass", todayInitialAuditPass); 
		map.put("yesterdayInitialAuditPass", yesterdayInitialAuditPass); 
		map.put("tswkInitialAuditPass", tswkInitialAuditPass); 
		map.put("monthInitialAuditPass", monthInitialAuditPass); 
		map.put("totalInitialAuditPass", totalInitialAuditPass); 
		
		Integer todayInitialAuditNopass = dataSummaryDao.selectTodayInitialAuditNopass();
		Integer yesterdayinitialAuditNopass = dataSummaryDao.selectYesterdayinitialAuditNopass();
		Integer tswkInitialAuditNopass = dataSummaryDao.selectTswkInitialAuditNopass();
		Integer monthInitialAuditNopass = dataSummaryDao.selectMonthInitialAuditNopass();
		Integer totalInitialAuditNopass = dataSummaryDao.selectTotalInitialAuditNopass();
		map.put("todayInitialAuditNopass", todayInitialAuditNopass); 
		map.put("yesterdayinitialAuditNopass", yesterdayinitialAuditNopass); 
		map.put("tswkInitialAuditNopass", tswkInitialAuditNopass); 
		map.put("monthInitialAuditNopass", monthInitialAuditNopass); 
		map.put("totalInitialAuditNopass", totalInitialAuditNopass);
		
		Integer todayReviewAuditPass = dataSummaryDao.selectTodayReviewAuditPass();
		Integer yesterdayReviewAuditPass = dataSummaryDao.selectYesterdayReviewAuditPass();
		Integer swkReviewAuditPass = dataSummaryDao.selecttSwkReviewAuditPass();
		Integer monthReviewAuditPass = dataSummaryDao.selectMonthReviewAuditPass();
		Integer totalReviewAuditPass = dataSummaryDao.selectTotalReviewAuditPass();
		map.put("todayReviewAuditPass", todayReviewAuditPass); 
		map.put("yesterdayReviewAuditPass", yesterdayReviewAuditPass); 
		map.put("swkReviewAuditPass", swkReviewAuditPass); 
		map.put("monthReviewAuditPass", monthReviewAuditPass); 
		map.put("totalReviewAuditPass", totalReviewAuditPass); 
		
		Integer todayReviewAuditNopass = dataSummaryDao.selectTodayReviewAuditNopass();
		Integer yesterdayReviewAuditNopass = dataSummaryDao.selectYesterdayReviewAuditNopass();
		Integer tswkReviewAuditNopass = dataSummaryDao.selectTswkReviewAuditNopass();
		Integer monthReviewAuditNopass = dataSummaryDao.selectMonthReviewAuditNopass();
		Integer totalReviewAuditNopass = dataSummaryDao.selectTotalReviewAuditNopass();
		map.put("todayReviewAuditNopass", todayReviewAuditNopass); 
		map.put("yesterdayReviewAuditNopass", yesterdayReviewAuditNopass); 
		map.put("tswkReviewAuditNopass", tswkReviewAuditNopass); 
		map.put("monthReviewAuditNopass", monthReviewAuditNopass); 
		map.put("totalReviewAuditNopass", totalReviewAuditNopass); 
		
		Integer todayFinalAuditPass = dataSummaryDao.selectTodayFinalAuditPass();
		Integer yesterdayFinalAuditPass = dataSummaryDao.selectYesterdayFinalAuditPass();
		Integer tswkFinalAuditPass = dataSummaryDao.selectTswkFinalAuditPass();
		Integer monthFinalAuditPass = dataSummaryDao.selectMonthFinalAuditPass();
		Integer totalFinalAuditPass = dataSummaryDao.selectTotalFinalAuditPass();
		map.put("todayFinalAuditPass", todayFinalAuditPass); 
		map.put("yesterdayFinalAuditPass", yesterdayFinalAuditPass); 
		map.put("tswkFinalAuditPass", tswkFinalAuditPass); 
		map.put("monthFinalAuditPass", monthFinalAuditPass); 
		map.put("totalFinalAuditPass", totalFinalAuditPass); 
		 
		Integer todayFinalAuditNopass = dataSummaryDao.selectTodayFinalAuditNopass();
		Integer yesterdayFinalAuditNopass = dataSummaryDao.selectYesterdayFinalAuditNopass();
		Integer tswkFinalAuditNopass = dataSummaryDao.selectTswkFinalAuditNopass();
		Integer monthFinalAuditNopass = dataSummaryDao.selectMonthFinalAuditNopass();
		Integer totalFinalAuditNopass = dataSummaryDao.selectTotalFinalAuditNopass();
		map.put("todayFinalAuditNopass", todayFinalAuditNopass); 
		map.put("yesterdayFinalAuditNopass", yesterdayFinalAuditNopass); 
		map.put("tswkFinalAuditNopass", tswkFinalAuditNopass); 
		map.put("monthFinalAuditNopass", monthFinalAuditNopass); 
		map.put("totalFinalAuditNopass", totalFinalAuditNopass); 
		
		BigDecimal todayLoanMoney = dataSummaryDao.selectTodayLoanMoney();
		BigDecimal yesterdayLoanMoney = dataSummaryDao.selectYesterdayLoanMoney();
		BigDecimal tswkLoanMoney = dataSummaryDao.selectTswkLoanMoney();
		BigDecimal monthLoanMoney = dataSummaryDao.selectMonthLoanMoney();
		BigDecimal totalLoanMoney = dataSummaryDao.selectTotalLoanMoney();
		map.put("todayLoanMoney", interceptDecimal(todayLoanMoney));
		map.put("yesterdayLoanMoney", interceptDecimal(yesterdayLoanMoney));
		map.put("tswkLoanMoney", interceptDecimal(tswkLoanMoney));
		map.put("monthLoanMoney", interceptDecimal(monthLoanMoney));
		map.put("totalLoanMoney", interceptDecimal(totalLoanMoney));
		
		Integer todayLoanCount = dataSummaryDao.selectTodayLoanCount();
		Integer yesterdayLoanCount = dataSummaryDao.selectYesterdayLoanCount();
		Integer tswkLoanCount = dataSummaryDao.selectTswkLoanCount();
		Integer monthLoanCount = dataSummaryDao.selectMonthLoanCount();
		Integer totalLoanCount = dataSummaryDao.selectTotalLoanCount();
		map.put("todayLoanCount", todayLoanCount); 
		map.put("yesterdayLoanCount", yesterdayLoanCount); 
		map.put("tswkLoanCount", tswkLoanCount); 
		map.put("monthLoanCount", monthLoanCount); 
		map.put("totalLoanCount", totalLoanCount); 
		
		BigDecimal todayProfitMoney = dataSummaryDao.selectTodayProfitMoney();
		BigDecimal yesterdayProfitMoney = dataSummaryDao.selectYesterdayProfitMoney();
		BigDecimal tswkProfitMoney = dataSummaryDao.selectTswkProfitMoney();
		BigDecimal monthProfitMoney = dataSummaryDao.selectMonthProfitMoney();
		BigDecimal totalProfitMoney = dataSummaryDao.selectTotalProfitMoney();
		map.put("todayProfitMoney", interceptDecimal(todayProfitMoney));
		map.put("yesterdayProfitMoney", interceptDecimal(yesterdayProfitMoney));
		map.put("tswkProfitMoney", interceptDecimal(tswkProfitMoney));
		map.put("monthProfitMoney", interceptDecimal(monthProfitMoney));
		map.put("totalProfitMoney", interceptDecimal(totalProfitMoney));
		 
		BigDecimal todayRepayCapital = dataSummaryDao.selectTodayRepayCapital();
		BigDecimal yesterdayRepayCapital = dataSummaryDao.selectYesterdayRepayCapital();
		BigDecimal tswkRepayCapital = dataSummaryDao.selectTswkRepayCapital();
		BigDecimal monthRepayCapital = dataSummaryDao.selectMonthRepayCapital();
		BigDecimal totalRepayCapital = dataSummaryDao.selectTotalRepayCapital();
		map.put("todayRepayCapital", interceptDecimal(todayRepayCapital));
		map.put("yesterdayRepayCapital", interceptDecimal(yesterdayRepayCapital));
		map.put("tswkRepayCapital", interceptDecimal(tswkRepayCapital));
		map.put("monthRepayCapital", interceptDecimal(monthRepayCapital));
		map.put("totalRepayCapital", interceptDecimal(totalRepayCapital));
		
		Integer todayRepayCount = dataSummaryDao.selectTodayRepayCount();
		Integer yesterdayRepayCount = dataSummaryDao.selectYesterdayRepayCount();
		Integer tswkRepayCount = dataSummaryDao.selectTswkRepayCount();
		Integer monthRepayCount = dataSummaryDao.selectMonthRepayCount();
		Integer totalRepayCount = dataSummaryDao.selectTotalRepayCount();
		map.put("todayRepayCount", todayRepayCount); 
		map.put("yesterdayRepayCount", yesterdayRepayCount); 
		map.put("tswkRepayCount", tswkRepayCount); 
		map.put("monthRepayCount", monthRepayCount); 
		map.put("totalRepayCount", totalRepayCount); 
		
		Integer notyetCount = dataSummaryDao.selectNotyetCount();
		BigDecimal notyetCapital = dataSummaryDao.selectNotyetCapital();
		Integer notyetOverdueCount = dataSummaryDao.selectNotyetOverdueCount();
		BigDecimal notyetOverdueCapital = dataSummaryDao.selectNotyetOverdueCapital();
		BigDecimal notyetOverdueLatefee = dataSummaryDao.selectNotyetOverdueLatefee();
		BigDecimal totalRepayLatefee = dataSummaryDao.selectTotalRepayLatefee();
		
		
		map.put("notyetCount", notyetCount); 
		map.put("notyetOverdueCount", notyetOverdueCount); 
		map.put("notyetCapital", interceptDecimal(notyetCapital));
		map.put("notyetOverdueCapital", interceptDecimal(notyetOverdueCapital));
		map.put("notyetOverdueLatefee", interceptDecimal(notyetOverdueLatefee));
		map.put("totalRepayLatefee", interceptDecimal(totalRepayLatefee));
		
		return map;
	}
}
